﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Threading;
using TouchLibEvents;
using System.Windows.Shapes;

namespace nl.tno.ict.multitouch.wpfmultitouch
{
    /// <summary>
    /// Adorner that disables all controls that fall under it
    /// </summary>
    public class FingersAdorner : Adorner
    {

        

        #region Properties

        /// <summary>
        /// list of framework elements that are shown to represent a finger on screen
        /// </summary>
        private Dictionary<long, FrameworkElement> cursors;
        private Dictionary<long, FingerObject> Fingers;

        /// <summary>
        /// Gets or sets the color to paint
        /// </summary>
        public Brush Color
        {
            get { return (Brush)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        /// <summary>
        /// Gets or sets the color to paint
        /// </summary>
        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color", typeof(Brush), typeof(FingersAdorner),
            new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("#7F4047F7")));


        /// <summary>
        /// Gets or sets the border 
        /// </summary>
        public Pen Border
        {
            get { return (Pen)GetValue(BorderProperty); }
            set { SetValue(BorderProperty, value); }
        }

        /// <summary>
        /// Gets or sets the border 
        /// </summary>
        public static readonly DependencyProperty BorderProperty =
            DependencyProperty.Register("Border", typeof(Pen), typeof(FingersAdorner),
            new UIPropertyMetadata(new Pen(Brushes.Gray, 1)));

        //the start point where to start drawing
        private static readonly Point startPoint =
            new Point(0, 0);

        /// <summary>
        /// Gets or sets the text to display 
        /// </summary>
        public string OverlayedText
        {
            get { return (string)GetValue(OverlayedTextProperty); }
            set { SetValue(OverlayedTextProperty, value); }
        }

        /// <summary>
        /// Gets or sets the text to display 
        /// </summary>
        public static readonly DependencyProperty OverlayedTextProperty =
            DependencyProperty.Register("OverlayedText", typeof(string), typeof(FingersAdorner), new UIPropertyMetadata(""));

        /// <summary>
        /// Gets or sets the foreground to use for the text
        /// </summary>
        public Brush ForeGround
        {
            get { return (Brush)GetValue(ForeGroundProperty); }
            set { SetValue(ForeGroundProperty, value); }
        }

        /// <summary>
        /// Gets or sets the foreground to use for the text
        /// </summary>
        public static readonly DependencyProperty ForeGroundProperty =
            DependencyProperty.Register("ForeGround", typeof(Brush), typeof(FingersAdorner),
            new UIPropertyMetadata(Brushes.Black));


        /// <summary>
        /// Gets or sets the font size for the text
        /// </summary>
        public double FontSize
        {
            get { return (double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }

        /// <summary>
        /// Gets or sets the font size for the text
        /// </summary>
        public static readonly DependencyProperty FontSizeProperty =
            DependencyProperty.Register("FontSize", typeof(double), typeof(FingersAdorner), new UIPropertyMetadata(10.0));


        /// <summary>
        /// Gets or sets the Typeface for the text
        /// </summary>
        public Typeface Typeface
        {
            get { return (Typeface)GetValue(TypefaceProperty); }
            set { SetValue(TypefaceProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Typeface for the text
        /// </summary>
        public static readonly DependencyProperty TypefaceProperty =
            DependencyProperty.Register("Typeface", typeof(Typeface), typeof(FingersAdorner),
            new UIPropertyMetadata(new Typeface("Verdana")));



        #endregion

        /// <summary>
        /// Constructor for the adorner
        /// </summary>
        /// <param name="adornerElement">The element to be adorned</param>
        public FingersAdorner(UIElement adornerElement)
            : base(adornerElement)
        {
            cursors = new Dictionary<long, FrameworkElement>();
            Fingers = new Dictionary<long,FingerObject>();
        }

        
        /// <summary>
        /// Called to draw on screen
        /// </summary>
        /// <param name="drawingContext">The drawind context in which we can draw</param>
        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {

            foreach (FingerObject fo in Fingers.Values)
            {

                drawingContext.DrawEllipse(Color, Border, new Point(fo.x, fo.y), 10.0, 10.0);
            }
            
            this.IsHitTestVisible = false;
            //FormattedText text = new FormattedText(OverlayedText, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight, Typeface, FontSize, ForeGround);
            //drawingContext.DrawText(text, startPoint);
            //drawingContext.DrawRectangle(Color, Border, new Rect(startPoint, DesiredSize));
            //drawingContext.DrawEllipse(Color, Border, new Point(100, 100), 10.0, 10.0);
            base.OnRender(drawingContext);
        }
        

        internal void RemoveIcon(long id)
        {
            //Ellipse e = _ellipseHash[(object)fo.Id];            
            
            if (Fingers.ContainsKey(id)) Fingers.Remove(id);
            this.InvalidateVisual();
            
            
        }

        private FrameworkElement GetDefaultFingerIcon(int size)
        {
            Ellipse FingerEllipse = new Ellipse();
            FingerEllipse.Fill = new SolidColorBrush(Colors.Black);
            FingerEllipse.Stroke = new SolidColorBrush(Colors.Black);
            FingerEllipse.Height = size;
            FingerEllipse.Width = size;
            FingerEllipse.StrokeThickness = 0;
            FingerEllipse.Opacity = 100;
            FingerEllipse.IsHitTestVisible = false;
            return FingerEllipse;
        }

        internal void AddIcon(FingerObject fo)
        {
            Fingers[fo.Id] = fo;
            
            // Register the Button with the element tree so that
            // it will be rendered, and can inherit DP values.
            //FrameworkElement fe = GetDefaultFingerIcon(20);
            //base.AddLogicalChild(fe);
            //base.AddVisualChild(fe);
            //fe.Margin = new Thickness(fo.LastPosition.X, fo.LastPosition.Y, 0, 0);
            this.InvalidateVisual();
        }
    }
}
